package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.entity.PersonInformation;
import com.yaojiaxun.logisticsPlatformSystem.service.PersonInformationService;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/2 20:43
 * @Description:
 */
@Controller
public class PersonInformationController {
    @Autowired
    PersonInformationService personInformationService;

    //人员管理首页
    @RequestMapping("/person_manage")
    public String personManageIndex(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        map.put("pageDTO", personInformationService.findPage(page));
        return "system/person/person_manage";
    }

    //人员管理首页Json
    @ResponseBody
    @RequestMapping("/person_manage_json")
    public ResultVO personManageIndexJson(@RequestParam(defaultValue = "1") Integer page){
        return ResultVOUtil.success(personInformationService.findPage(page));
    }

    //人员详细信息
    @RequestMapping("/person_detail")
    public String getDetail(@RequestParam("personId") Long personId, @RequestParam("page") Integer page, Map<String, Object> map){
        map.put("page", page);
        map.put("personInfo", personInformationService.findPersonById(personId));
        return "system/person/person_detail";
    }

    //修改人员信息完毕后跳转到人员管理首页
    @RequestMapping("/person_update_after")
    public String updatePersonAfter(@RequestParam("page") Integer page, PersonInformation personInformation){
        //System.out.println(personInformation);
        personInformationService.updatePerson(personInformation);
        return "redirect:/person_manage?page="+page;
    }

    //删除人员
    @RequestMapping("/personDelete")
    public String deletePerson(@RequestParam("personId") Long personId, @RequestParam("page") Integer page){
        personInformationService.deletePerson(personId);
        return "redirect:/person_manage?page="+page;
    }

    //添加人员信息前
    @RequestMapping("/personAddBefore")
    public String addPersonBefore(Map<String, Object> map){
        return "system/person/person_add";
    }

    //添加人员信息后
    @RequestMapping("/person_add_after")
    public String addPersonAfter(PersonInformation personInformation){
        personInformationService.insertPerson(personInformation);
        return "redirect:/person_manage";
    }

    //查找所有人员
    @ResponseBody
    @RequestMapping("/findAllPerson")
    public ResultVO findAllPerson(){
        return ResultVOUtil.success(personInformationService.findAllPerson());
    }

    //按姓名查询(可能有多个结果)
    @RequestMapping("/findPersonByName")
    public String findPersonByName(@RequestParam("personName") String personName, Map<String, Object> map){
        map.put("results", personInformationService.findPersonByName(personName));
        return "system/person/person_search_results";
    }
}
